package weibo4j;

import java.util.ArrayList;
import java.util.List;

import weibo4j.http.Response;
import weibo4j.org.json.JSONArray;
import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;

public class Emotion extends WeiboResponse implements java.io.Serializable {
	/**
	 * <emotions> <emtion> <phrase>[嘻嘻]</phrase> <type>image</type>
	 * <url>http://timg
	 * .sjs.sinajs.cn/miniblog2style/images/common/face/ext/normal
	 * /c2/tooth.gif</url> <is_hot>false</is_hot> <is_common>true</is_common>
	 * <order_number>96</order_number>\ <category>表情</category> </emotion>
	 * <emtion> <phrase>[呵呵]</phrase> <type>image</type>
	 * <url>http://timg.sjs.sinajs
	 * .cn/miniblog2style/images/common/face/ext/normal/eb/smile.gif</url>
	 * <is_hot>false</is_hot> <is_common>true</is_common>
	 * <order_number>95</order_number>\ <category>表情</category> </emotion>
	 * ...... </emotions> / /**
	 * 
	 */
	private static final long serialVersionUID = 7786378967827789387L;
	private Weibo weibo;
	private String phrase;
	private String type;
	private String url;
	private boolean isHot;
	private boolean isCommon;
	private int orderNumber;
	private String category;

	Emotion(JSONObject json) throws WeiboException {
		// TODO Auto-generated constructor stub
		super();
		init(json);
	}

	public String getPhrase() {
		return phrase;
	}

	public String getType() {
		return type;
	}

	public String getURL() {
		return url;
	}

	public boolean isHot() {
		return isHot;
	}

	public boolean isCommon() {
		return isCommon;
	}

	public String getCategory() {
		return category;
	}

	public int getOrderNumber() {
		return orderNumber;
	}

	private void init(JSONObject json) throws WeiboException {
		try {
			phrase = json.getString("phrase");
			type = (json.getString("type"));
			url = (json.getString("url"));
			isHot = (json.getBoolean("is_hot"));
			isCommon = (json.getBoolean("is_common"));
			orderNumber = (json.getInt("order_number"));
			category = (json.getString("category"));
		} catch (JSONException jsone) {
			throw new WeiboException(
					jsone.getMessage() + ":" + json.toString(), jsone);
		}
	}

	/**
	 * @param res
	 * @return
	 * @throws WeiboException
	 */
	static List<Emotion> constructResult(Response res) throws WeiboException {
		JSONArray list = res.asJSONArray();
		try {
			int size = list.length();
			List<Emotion> emontions = new ArrayList<Emotion>(size);
			for (int i = 0; i < size; i++) {
				emontions.add(new Emotion(list.getJSONObject(i)));
			}
			return emontions;
		} catch (JSONException e) {
		}
		return null;
	}

	 @Override
	 public int hashCode() {
		 return orderNumber;
	 }
	
	 @Override
	 public boolean equals(Object obj) {
		 if (null == obj) {
			 return false;
		 }
		 if (this == obj) {
			 return true;
		 }
		 return obj instanceof Emotion && ((Emotion) obj).orderNumber == this.orderNumber;
	 }

	public String toString() {
		return "Emotion{" + "weibo=" + weibo + ", phrase=" + phrase
				+ ", type='" + type + '\'' + ", url='" + url + '\''
				+ ", isHot='" + isHot + '\'' + ", isCommon='" + isCommon + '\''
				+ ", orderNumber='" + orderNumber + '\'' + ", category='"
				+ category + '}';
	}
	
	public String getSQLString() {
		return "phrase='" + phrase + '\'' + 
			   ", type='" + type + '\'' + 
			   ", url='" + url + '\'' + 
			   ", is_hot='" + (isHot == true ? 1 : 0)+ '\'' + 
			   ", is_common='" + (isCommon == true ? 1 : 0) + '\'' + 
			   ", order_number=" + orderNumber+ 
			   ", category='" + category + '\'';
	}

}
